package com.yxkj.syh.app.huarong.activities.user.setting.pwd;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.ActivityManager;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.data_center.model.AccountModel;
import com.yxkj.syh.app.huarong.util.UserManager;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 创建者：SYH
 * 时间：2020/5/21 10:41
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class EditPwdVM extends BaseViewModel<ActivityEvent> {

    private Disposable mDisposable;

    public EditPwdVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofForm1 = new ObservableField<>(UserManager.getUserManager().getUser().getPhone());
    public ObservableField<String> ofForm2 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm3.get(), ofForm5.get(), ofForm4.get());
        }
    };
    public ObservableField<String> ofForm3 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm2.get(), ofForm5.get(), ofForm4.get());
        }
    };
    public ObservableField<String> ofForm4 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm2.get(), ofForm5.get(), ofForm3.get());
        }
    };
    public ObservableField<String> ofForm5 = new ObservableField<String>() {
        @Override
        public void set(String value) {
            super.set(value);
            commitEnable(value, ofForm3.get(), ofForm2.get(), ofForm4.get());
        }
    };
    public ObservableField<String> ofVerCode = new ObservableField<>(getApplication().getString(R.string.get_ver_code));
    public ObservableField<Boolean> ofVerCodeEnable = new ObservableField<>(Boolean.TRUE);
    public ObservableField<Boolean> ofCommitEnable = new ObservableField<>(Boolean.FALSE);

    private void commitEnable(String... strings) {
        if (!TextUtils.isEmpty(strings[0]) && !TextUtils.isEmpty(strings[1]) && !TextUtils.isEmpty(strings[2]) && !TextUtils.isEmpty(strings[3])) {
            if (!ofCommitEnable.get()) {
                ofCommitEnable.set(true);
            }
        } else {
            if (ofCommitEnable.get()) {
                ofCommitEnable.set(false);
            }
        }
    }

    public ClickEvent verCodeClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            verCode();
        }
    };

    private void verCode() {
        showLoading("verCode");
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("mobile", UserManager.getUserManager().getUser().getPhone());
        AccountModel.getAccountModel().verCode(mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("verCode");
                Tooast.success("请注意查收验证码");
                getVerCodeTimer();
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("verCode");
                Tooast.warning(msg);
            }
        });
    }

    public ClickEvent commitClick = new ClickEvent() {
        @Override
        public void onClick(View view) {

            if (ofForm4.get().equals(ofForm5.get())) {

                Map<String, Object> pMap = new HashMap<>();
                pMap.put("code", ofForm2.get());
                pMap.put("oldPassword", ofForm3.get());
                pMap.put("newPassword", ofForm4.get());
                pMap.put("confirmPassword", ofForm5.get());

                AccountModel.getAccountModel().setLoginPwd(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        Tooast.success("修改成功");
                        ARouter.getInstance().build(ArouterPath.LOGIN_ACTIVITY).navigation();
                        ActivityManager.getInstance().finishAllActivity();
                    }

                    @Override
                    public void onFailed(int code, String msg) {
                        Tooast.warning(msg);
                    }
                });
            } else {
                Tooast.normalInfo("新密码与确认密码不一致");
            }

        }
    };


    @SuppressLint("CheckResult")
    private void getVerCodeTimer() {
        Observable.interval(0, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                        ofVerCodeEnable.set(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if (60 - aLong > 0) {
                            ofVerCode.set(60 - aLong + "s " + getApplication().getString(R.string.reget));
                        } else {
                            ofVerCode.set(getApplication().getString(R.string.get_ver_code));
                            ofVerCodeEnable.set(true);
                            mDisposable.dispose();
                            mDisposable = null;
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mDisposable.dispose();
                        mDisposable = null;
                    }

                    @Override
                    public void onComplete() {
                        mDisposable.dispose();
                        mDisposable = null;
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }
}
